<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of welcome
 *
 * @author Obi
 */
class Admin_pengiriman extends MY_Controller {

    function __construct() {
        parent::__construct();

        $cek_admin = $this->session->userdata('cek_admin');
        if (!$cek_admin)
            redirect("admin");


        $this->mmitrapengiriman = new M_mitra_pengiriman();
        $this->mtarif_kirim = new M_tarif_kirim();
        $this->mkota = new M_kota();
    }

    function index() {

        $data['title'] = 'Daftar Mitra Pengiriman';
        $data['list'] = $this->mmitrapengiriman->get();

        $data['isi'] = 'isi';
        $this->load->view('template_admin', $data);
    }

    function tambah() {

        $data['title'] = 'Tambah Mitra Pengiriman';

        $data['id'] = "";
        $data['kode_mitra'] = "";
        $data['nama_mitra'] = "";


        $data['isi'] = 'form';
        $this->load->view('template_admin', $data);
    }

    function edit($id = NULL) {

        $data['title'] = 'Ubah Katagori';

        $dt = $this->mmitrapengiriman->where("id = '$id'")->get();

        $data['id'] = $dt->id;
        $data['kode_mitra'] = $dt->kode_mitra;
        $data['nama_mitra'] = $dt->nama_mitra;

        $data['isi'] = 'form';
        $this->load->view('template_admin', $data);
    }

    function delete($id) {
        $this->mmitrapengiriman->delete($id);
        redirect("admin_pengiriman");
    }

    function simpan() {
        $x = 0;
        $id = $this->input->post('id');
        if ($id == "") {
            $n = $this->mmitrapengiriman->jml_mitra();
            $id = $n->jml + 1;
            $x = 1;
        }

        $data = array(
            'id' => $id,
            'kode_mitra' => $this->input->post('kode_mitra'),
            'nama_mitra' => $this->input->post('nama_mitra'),
        );

        if ($x == 1) {
            $this->mmitrapengiriman->insert($data);

            //case otomatis langsung ada biaya kirim per kota dari mitra
            $data = array(
                'kode_mitra' => $this->input->post('kode_mitra'),
                'nama_mitra' => $this->input->post('nama_mitra'),
            );

            $dta_kota = $this->mkota->get();
            foreach ($dta_kota as $dt) {
                $masukan_biaya = array(
                    'harga_kirim' => 0,
                    'id_mitra' => $id,
                    'id_kota' => $dt->id
                );
                $this->mtarif_kirim->insert($masukan_biaya);
            }
        } else {
            $this->mmitrapengiriman->update($data, $id);
        }

        redirect("admin_pengiriman");
    }

}

?>
